/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.util.writer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/** A class representing TableWriter
 * @author CENOURA!!!
 * @since 08/11/2005
 */
public class TableWriter {
    
    /** The list of Node Labels to output*/
    protected ArrayList<String> outPutNodeLabels = new ArrayList<String>();
    
    /** The list of Element Labels to output*/
    protected ArrayList<String> outPutElementLabels = new ArrayList<String>();
    
    /** The Map of Nodes and variables to output*/
    protected HashMap<String, int[]> outPutNodes = new HashMap<String, int[]>(2);
    
    /** The Map of Elements and IPs, Mps, variables to output*/
    protected HashMap<String, Object> outPutElements = new HashMap<String, Object>(2);
    
    public int getNumberOfOutPutNodes(){
        return (outPutNodeLabels.size());
    };
    
    public ArrayList getOutPutNodeLabelsList(){
        return (outPutNodeLabels);
    };
    
    public String getOutPutNodeLabel(int a) {
        return (String)outPutNodeLabels.get(a);
    };
    
    public void addNodeLabel(String s) {
        outPutNodeLabels.add(s);
    };
    
    public void removeNodeLabel(int a) {
        outPutNodeLabels.remove(a);
    };
    
    public int getNumberOfOutPutElements(){
        return (outPutElementLabels.size());
    };
    
    public ArrayList getOutPutElementLabelsList(){
        return (outPutElementLabels);
    };
    
    public String getOutPutElementLabel(int a) {
        return (String)outPutElementLabels.get(a);
    };
    
    public void addElementLabel(String s) {
        outPutElementLabels.add(s);
    };
    
    public void removeElementLabel(int a) {
        outPutElementLabels.remove(a);
    };
    
    public int[] getOutPutNodesIntArray(String key) {
          return (int[])outPutNodes.get(key);
      }
    
    public int getOutPutNodesInt(String key, int i) {
        return this.getOutPutNodesIntArray(key)[i];
    }
    
    public boolean outPutNodesContainsKey(String key) {
          return outPutNodes.containsKey(key);
      }
     
    public void setOutPutNodes(String key, int[] iarray) {
        outPutNodes.put(key, iarray);
      }
    
    public boolean outPutElementsContainsKey(String key) {
        return outPutElements.containsKey(key);
    }
   
    public void setOutPutElements(String key, HashMap hm) {
        outPutElements.put(key, hm);
    }
    
    public void setOutPutElements(String key, ArrayList al) {
        outPutElements.put(key, al);
    }
    
    public HashMap getOutPutElementsIPsMap(String elem) {
        return (HashMap)outPutElements.get(elem);
    }
    
    public ArrayList getOutPutElementIPsLabelsArray() {
        return (ArrayList)outPutElements.get("LABELS");
    }
    
    public String getOutPutElementIPLabel(int i) {
        return (String)((ArrayList)outPutElements.get("LABELS")).get(i);
    }
    
    public int getOutPutElementNumberOfIPs() {
        return ((ArrayList)outPutElements.get("LABELS")).size();
    }
    
    public HashMap getOutPutElementsIPMPMap(String elem, String ip) {
        return (HashMap)((HashMap)outPutElements.get(elem)).get(ip);
    }
    
    public ArrayList getOutPutElementIPMPLabelsArray(String elem) {
        return (ArrayList)((HashMap)outPutElements.get(elem)).get("LABELS");
    }
    
    public String getOutPutElementIPMPLabel(String elem, int i) {
        return (String)((ArrayList)((HashMap)outPutElements.get(elem)).get("LABELS")).get(i);
    }
    
    public int getOutPutElementIPNumberOfMPs(String elem) {
        return ((ArrayList)((HashMap)outPutElements.get(elem)).get("LABELS")).size();
    }
    
    public int[] getOutPutElementIPMPVariablesIntArray(String elem, String ip, String mp) {
        return (int[])((HashMap)((HashMap)outPutElements.get(elem)).get(ip)).get(mp);
    }
    
    public int getOutPutElementIPMPVariablesInt(String elem, String ip, String mp, int i) {
        return ((int[])((HashMap)((HashMap)outPutElements.get(elem)).get(ip)).get(mp))[i];
    }
    
    public int getOutPutElementTotalNumberOfMPs() {
        int q = 0;
        for (int i = 0; i < this.getNumberOfOutPutElements(); i++) {
            for (int j = 0; j < this.getOutPutElementNumberOfIPs(); j++) {
                for (int k = 0; k < this.getOutPutElementIPNumberOfMPs(this.getOutPutElementLabel(i)); k++) {
                    q++;
                }
            }
        }
        return q;
    }
    
    public int getOutPutElementTotalNumberOfIPs() {
        int q = 0;
        for (int i = 0; i < this.getNumberOfOutPutElements(); i++) {
            for (int j = 0; j < this.getOutPutElementNumberOfIPs(); j++) {
                    q++;
            }
        }
        return q;
    }
    
    /** Writes a string at a file.
     * @param f The file.
     * @param s the String to write.
     */
    public void writer(File f, String s){
        try {
            FileWriter w = new FileWriter(f);
            char[]c=s.toCharArray();
            for (int i = 0; i < c.length; i++) {
                w.write(c[i]);
            }
            w.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
